//
//  WmcCancellationFeedbackVC.swift
//  YPWatermarkCamera
//
//  Created by 马涛 on 2022/6/20.
//

import UIKit
import YPBedrock

// MARK: - 扩展WmcCancellationFeedbackVC + WmcNavigatorProtocol

extension WmcCancellationFeedbackVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.cancellationFeedback]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        let vc = WmcCancellationFeedbackVC()
        return vc
    }
}

// MARK: - 扩展WmcCancellationFeedbackVC + WmcVMProtocol

extension WmcCancellationFeedbackVC: WmcVMProtocol {
    typealias VMType = WmcCancellationFeedbackVM
}

// MARK: - [WmcCancellationFeedbackVC]

class WmcCancellationFeedbackVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "注销账号"
    }

    // Internal

    lazy var scrollView: UIScrollView = {
        let view = UIScrollView()
        return view
    }()

    lazy var reasonTagLabel: UILabel = {
        let view = UILabel()
        view.font = 18.yp.uiFont(weight: .bold)
        view.textColor = UIColor.wmc_blackH1_000000
        let attriStr = "注销账号原因".yp.makeAttributed { make in
            make.set(font: 18.yp.uiFont(weight: .bold))
                .set(foregroundColor:UIColor.wmc_blackH1_000000)
        }
        let subS = "(必填):".yp.makeAttributed { make in
            make.set(font: 15.yp.uiFont(weight: .regular))
                .set(foregroundColor:UIColor.wmc_mainTheme)
        }
        attriStr.append(subS)
        view.attributedText = attriStr
        return view
    }()

    lazy var textView: WmcCancellationReasonTextView = {
        let view = WmcCancellationReasonTextView(placeholder: "请具体描述注销原因", maxCount: 300)
        view.backgroundColor = UIColor.wmc_white_FFFFFF
        return view
    }()

    lazy var uploadTagLabebl: UILabel = {
        let view = UILabel()
        view.font = 18.yp.uiFont(weight: .bold)
        view.textColor = UIColor.wmc_blackH1_000000
        view.text = "上传图片:"
        return view
    }()

    lazy var uploadView: WmcUploadGridView = {
        let view = WmcUploadGridView(row: 2, column: 4)
        return view
    }()

    lazy var confirmButton: UIButton = {
        let view = UIButton()
        view.setTitle("确认注销", for: .normal)
        view.setTitleColor(UIColor.wmc_white_FFFFFF, for: .normal)
        view.backgroundColor = UIColor.wmc_grayH16_EBEBEB
        view.layer.cornerRadius = 3
        view.clipsToBounds = true
        return view
    }()

    override func makeUI() {
        makeNavView(showShadow: false)

        addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }

        scrollView.addSubview(reasonTagLabel)
        scrollView.addSubview(textView)
        scrollView.addSubview(uploadTagLabebl)
        scrollView.addSubview(uploadView)
        scrollView.addSubview(confirmButton)

        reasonTagLabel.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(13)
            make.right.equalTo(-13)
            make.width.equalTo(Wmc_screenWidth - 26)
        }
        textView.snp.makeConstraints { make in
            make.top.equalTo(reasonTagLabel.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(160)
        }
        uploadTagLabebl.snp.makeConstraints { make in
            make.top.equalTo(textView.snp.bottom).offset(15)
            make.left.equalTo(13)
        }
        uploadView.snp.makeConstraints { make in
            make.top.equalTo(uploadTagLabebl.snp.bottom).offset(15)
            make.left.equalTo(13)
            make.right.equalTo(-13)
        }
        confirmButton.snp.makeConstraints { make in
            make.top.equalTo(uploadView.snp.bottom).offset(87)
            make.left.equalTo(13)
            make.right.equalTo(-13)
            make.height.equalTo(42)
            make.bottom.equalTo(-100)
        }
    }

    /// 变化函数
    /// - Parameter str: str description
    /// - Returns: description
    func transformString(str: String?) -> UIColor {
        if let t = str {
            if t.count > 0 {
                return UIColor.wmc_mainTheme
            } else {
                return UIColor.wmc_grayH16_EBEBEB
            }
        } else {
            return UIColor.wmc_grayH16_EBEBEB
        }
    }

    /// 检查长度是否大于0
    /// - Parameter str: str description
    /// - Returns: description
    func checkString(str: String?) -> Bool {
        if let t = str {
            if t.count > 0 {
                return true
            } else {
                return false
            }
        } else {
            return false
        }
    }

    override func bindViewModel() {
        textView.textView.rx.text.map { [weak self] text in
            guard let weakSelf = self else { return UIColor.wmc_grayH16_EBEBEB }
            return weakSelf.transformString(str: text)
        }.bind(to: confirmButton.rx.backgroundColor).disposed(by: rx.disposeBag)

        textView.textView.rx.text.map { [weak self] text in
            guard let weakSelf = self else { return false }
            return weakSelf.checkString(str: text)
        }.bind(to: confirmButton.rx.isEnabled).disposed(by: rx.disposeBag)

        textView.textView.rx.text.bind { [weak self] text in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.reason = text
        }.disposed(by: rx.disposeBag)

        uploadView.selectedModels.bind { [weak self] imgs in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.imgs = imgs
            WmcLogManager.Log("选择的图片=====\(imgs)")
        }.disposed(by: rx.disposeBag)

        viewModel.cancelResult.bind { [weak self] result in
            guard let weakSelf = self, let res = result else { return }
            weakSelf.showAlert(res: res)
        }.disposed(by: rx.disposeBag)
    }

    override func bindViewAction() {
        confirmButton.yp.addEvent() { [weak self] _ in
            guard let weakSelf = self else { return }
            /// 上传图片跳转下一步
            /// 检查输入有效性
            if !weakSelf.viewModel.isInputValidate() {
                // 此处使用系统alert
                weakSelf.showAlert(text: "请填写注销原因，不少于15个字，且必须含有汉字")
            } else {
                weakSelf.viewModel.cancelAccount()
            }
        }
    }

    /// 显示弹窗
    /// - Parameter res: res description
    /// - Returns: description
    func showAlert(res: WmcCancellationResultModel) {
        var integral: String?
        if let temp = res.data?.integral {
            if temp == 0 {
                integral = nil
            } else {
                integral = "\(temp)"
            }
        }
        var reason = ""
        if let msg = res.head?.msg {
            reason = msg
        }
        showAlert(integral: integral, text: reason)
    }

    func showAlert(integral: String?, text: String) {
        let alert = WmcCancellationAlertView(integral: integral, content: text)
        alert.show()

        alert.rethinkButton.yp.addEvent { _ in
            alert.dismiss()
        }

        alert.cancallation.yp.addEvent { _ in
            WmcLogManager.Log("确认注销账号+++")
            alert.dismiss(animated: true) { [weak self] in
                guard let weakSelf = self else { return }
                WmcNavigatorManager.push(.cancellationValidation, parms: weakSelf.viewModel)
            }
        }
    }

    func showAlert(text: String) {
        let alert = WmcAlertView()
        alert.title = "系统提示"
        alert.msg = text
        alert.style = .confirm(title: "确定")
        alert.show()
    }
}
